<?php
class kucun_productModel extends model{
	public $base;
	public function __construct(&$base){
		parent::__construct($base);
		$this->base=$base;
		$this->table="kucun_product";
	}
	
	
	 
	public function id_list($option){
		$data=$this->select($option);
		if($data){
			foreach($data as $k=>$v){
				$t[$v['id']]=$v;
			}
			return $t;
		}
		return false;
	}
	
	public function GetProductIdsBySHop($option){
		$data=$this->select($option);
		if($data){
			foreach($data as $k=>$v){
				$t[$v['productid']]=$v;
			}
			return $t;
		}
		return false;
	}
	
	 
	
	public function listbyshop($shopid){
		$option=array(
			"where"=>" shopid=".$shopid
		);
		$data=$this->select($option,$rscount);
		if($data){
			foreach($data as $k=>$v){
				$productids[]=$v['productod'];
			}
			$products=M('product')->getListByIds($productids);
			foreach($data as $k=>$v){
				$v['title']=$products[$v['product']]['title'];
			}
		}
	}
	
	public function getlistbyshop($shopid){
		$option=array(
			"where"=>" shopid=".$shopid
		);
		$data=$this->select($option,$rscount);
		if($data){
			foreach($data as $k=>$v){
				$productids[]=$v['productid'];
			}
			$products=M('product')->getListByIds($productids);
			foreach($data as $k=>$v){
				$v['title']=$products[$v['productid']]['title'];
				$data[$k]=$v;
			}
		}
		return $data;
	}
	public function statDayAdd($option){
		$where=" productid=".$option['productid']." AND k='".$option['k']."' AND kday='".date("Ymd")."' AND siteid=".$option['siteid']." AND shopid=".$option['shopid']." ";
		
		$row=M("stat_product_day")->selectRow($where);
		if($row){
			
			M("stat_product_day")->update(array(
				"ordernum"=>$row['ordernum']+$option['ordernum']
			),"id=".$row['id']);
		}else{
			M("stat_product_day")->insert($option);
		}		
		
	}
	
	public function statWeekAdd($option){
		$where=" productid=".$option['productid']." AND k='".$option['k']."' AND kweek='".date("YW")."' AND siteid=".$option['siteid']." AND shopid=".$option['shopid']." ";
		
		$row=M("stat_product_week")->selectRow($where);
		if($row){
			
			M("stat_product_week")->update(array(
				"ordernum"=>$row['ordernum']+$option['ordernum']
			),"id=".$row['id']);
		}else{
			M("stat_product_week")->insert($option);
		}		
		
	}
	
	public function statMonthAdd($option){
		
		$where=" productid=".$option['productid']." AND k='".$option['k']."' AND kmonth='".date("Ym")."' AND siteid=".$option['siteid']." AND shopid=".$option['shopid']." ";
		
		$row=M("stat_product_month")->selectRow($where);
		if($row){
			
			M("stat_product_month")->update(array(
				"ordernum"=>$row['ordernum']+$option['ordernum']
			),"id=".$row['id']);
		}else{
			M("stat_product_month")->insert($option);
		}		
		
	}
	
	/**
	*夜猫店 库存处理
	*
		array(
			"shopid"=>$shopid,
			"productid"=>$productid	,
			"total_num"=>$total_num,
			"content"=>"",
		)
	*/
	public function total_num_add($option){
		$shopid=intval($option['shopid']);
		$productid=intval($option['productid']);
		$total_num=intval($option['total_num']);
		$content=$option['content'];
		$row=M("kucun_product")->selectRow("shopid=".$shopid." AND productid=".$productid."");
		$product=M("product")->selectRow("id=".$productid);
		if(empty($row)){
			M("kucun_product")->insert(array(
				"shopid"=>$shopid,
				"productid"=>$productid,
				"total_num"=>$total_num,
				"dateline"=>time(),
				"jh_price"=>$product['gys_price'],
				 
			));
			$row['total_num']=0;
		}else{
			M("kucun_product")->update(array(
				"total_num"=>$row['total_num']+$total_num
			),"id=".$row['id']);
		}
		//处理记录
		M("kucun_kclog")->insert(array(
			"shopid"=>$shopid,
			"productid"=>$productid,
			"dateline"=>time(),
			"total_num"=>$total_num,
			"content"=>$content.$product['title']."库存变更".$total_num.",原来".$row['total_num'].",现在".($row['total_num']+$total_num)."",
		));
		
		
	}
	 
	
	
	
	
}

?>